//
//  SPIntelligentServiceConfig.m
//  BaseProject
//
//  Created by Sailer Guo on 2019/11/28.
//  Copyright © 2019 Base. All rights reserved.
//

#import "SPIntelligentServiceConfig.h"

@implementation SPIntelligentServiceConfig


+ (SPIntelligentServiceConfig *)serviceConfigWith:(EnvironmentType)env {
    switch (env) {
        case Local:
            return [self localServiceConfig];
            break;
        
        case Development:
            return [self developmentServiceCofig];
            
        case Production:
            return [self productionServiceConfig];
            break;
        default:
            NSAssert(NO, @"Unknown Env");
            return nil;
            break;
    }
}

+ (SPIntelligentServiceConfig *)productionServiceConfig {
    SPIntelligentServiceConfig *serviceConfig = [[SPIntelligentServiceConfig alloc] init];
    serviceConfig.baseUrl = @"https://app.shopin.cn/community/";
    serviceConfig.baseImageUrl = @"http://images.shopin.net/images";
    [serviceConfig configSession];
    return serviceConfig;
}

+(SPIntelligentServiceConfig *)localServiceConfig {
    SPIntelligentServiceConfig *serviceConfig = [[SPIntelligentServiceConfig alloc] init];
    serviceConfig.baseUrl = @"http://192.168.110.179:10480/";
    serviceConfig.baseImageUrl = @"http://images.shopin.net/images";
    [serviceConfig configSession];
    return serviceConfig;
}

+ (SPIntelligentServiceConfig *)developmentServiceCofig {
    SPIntelligentServiceConfig *serviceConfig = [[SPIntelligentServiceConfig alloc] init];
    serviceConfig.baseUrl = @"http://192.168.110.179:10480/";
    serviceConfig.baseImageUrl = @"http://images.shopin.net/images";
    [serviceConfig configSession];
    return serviceConfig;
}

- (void)configSession {
    AFHTTPSessionManager *httpRequestManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:self.baseUrl] sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableSet *contentTypes = [NSMutableSet setWithSet:httpRequestManager.responseSerializer.acceptableContentTypes];
    
    [contentTypes addObject:@"content-type"];
    [contentTypes addObject:@"application/json;charse=UTF-8"];
    
    httpRequestManager.responseSerializer.acceptableContentTypes = contentTypes;
    
    httpRequestManager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    httpRequestManager.securityPolicy.allowInvalidCertificates = YES;
    
    AFSecurityPolicy *securityPoliy = [AFSecurityPolicy defaultPolicy];
    //allowInvalidCertificates 是否允许无效证书（也就是自建的证书），默认为NO
    //如果是需要验证自建证书，需要设置为YES
    securityPoliy.allowInvalidCertificates = YES;
    securityPoliy.validatesDomainName = NO;
    httpRequestManager.securityPolicy = securityPoliy;
    
    session = httpRequestManager;
}

- (BOOL)successWitResponseObject:(id)responseObject {
    NSString *errorCode = responseObject[@"code"];
    if (errorCode && [errorCode isEqualToString:@"A00000"]) {
        return YES;
    }
    
    return [responseObject[@"success"] boolValue];
}

/*
 commuinty 可能返回两种数据格式，都要处理
 第一种
 { "code": "A00000", "data": true, "errorMessage": "成功" }
 
 第二种
 { "success":true, "body": {}, }
 */
- (id)responseData:(id)responseObject withUrl:(nonnull NSString *)url isList:(BOOL)isList {
    
    NSSet *set = [NSSet setWithObjects:@"member/selectDailysign",nil];
    
    if ([set containsObject:url]) {
        return responseObject;
    }
    
    id dataBody = responseObject[@"body"];
    if (dataBody) {
        if (isList) {
            return responseObject[@"body"][@"page"][@"list"];
        } else {
            return responseObject[@"body"];
        }
    }
    
    id data = responseObject[@"data"];
    return data;
}

- (NSError *)errorMessage:(id)responseObject withUrl:(nonnull NSString *)url{
    NSError *error = nil;
    NSString *errorMessage = responseObject[@"errorMessage"];
    if (errorMessage) {
        NSDictionary *userInfo = @{@"errorMessage":responseObject[@"errorMessage"]};
        error = [NSError errorWithDomain:NSErrorDomainNoAdaptiveData code:NSSPErrorCode userInfo:userInfo];
        return error;
    }
    
    error = [NSError errorWithDomain:NSErrorDomainNoAdaptiveData code:NSSPErrorCode userInfo:nil];
    return error;
}

@synthesize baseUrl;

@synthesize env;

@synthesize session;

@synthesize baseImageUrl;

@end
